<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./lib/bootstrap.css">
  <style>
    .badge {
      float: right;
      margin-right: 5px;
    }

    .my-page {
      margin: 0 5px;
    }

    .all-page-content {
      margin-left: 5px;
    }

    .mt15 {
      margin-top: 15px;
    }
  </style>
</head>

<body style="padding: 15px;">

  <!-- 评论面板 -->
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h3 class="panel-title">发表评论</h3>
    </div>
    <form class="panel-body cmt-form">
      <div>评论人：</div>
      <input type="text" class="form-control username" name="username" autocomplete="off" />
      <div>评论内容：</div>
      <textarea class="form-control content" name="content"></textarea>
      <button type="submit" class="btn btn-primary submit">发表评论</button>
    </form>
  </div>


  <!-- 评论列表 -->
  <ul class="mt15 list-group">
    <!-- <li class="list-group-item">
       <span>评论内容</span>
       <span class="badge del" style="cursor:pointer; background-color: lightgray;">删除</span> 
       <span class="badge" style="background-color: #F0AD4E;">评论时间：xxx</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：xxx</span>
    </li> -->
  </ul>

  <!-- 分页器 -->
  <nav class="mt15">
    <ul class="pagination">
      <li>
        <button class="last">
          <span>&laquo;</span>
        </button>
      </li>
      <li class="my-page">
        <span class="page-show"></span>
      </li>
      <li>
        <button class="next">
          <span>&raquo;</span>
        </button>
      </li>
      <li class="all-page-content">
        <span>共计:<span class="all-page"></span>页</span>
      </li>
    </ul>
  </nav>
  <script src="./lib/axios.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script src="./lib/pxmu.min.js"></script>
  <script>
    let page = 1
    let zpage

    // 提取数据信息
    async function getName() {
      let getdata = await axios({ url: `https://hmajax.itheima.net/api/cmtlist?page=${page}` })
      // console.log(getdata.data);
      let data = getdata.data
      return data
    }


    // 翻页函数效果
    async function pagination() {
      let data = await getName()
      console.log(data);
      zpage = data.allPage
      // console.log(zpage);
      let str = `<span>共计:<span class="all-page">${zpage}</span>页</span>`
      document.querySelector('.all-page-content').innerHTML = str
    }
    pagination()
    // 点按钮设置点击事件，转动页面
    document.querySelector('.last').addEventListener('click', function (e) {
      e.preventDefault()
      if (page > 1) {
        pxmu.loading('加载中...')
        page--
        pxmu.closeload()
        // document.querySelector('.page-show').innerHTML = page
        xuanran()
      }
    })
    document.querySelector('.next').addEventListener('click', function (e) {
      e.preventDefault()
      if (page < zpage) {
        pxmu.loading('加载中...')
        page++
        pxmu.closeload()
        xuanran()
      }
    })

    // 渲染页面
    async function xuanran() {
      let data = await getName()
      // console.log(res);
      let str = ''
      data.data.forEach(item => {
        str += `
          <li class="list-group-item">
            <span>${item.content}</span>
            <span data-id="${item.id}" class="badge del" style="cursor:pointer; background-color: lightgray;">删除</span> 
            <span class="badge" style="background-color: #F0AD4E;">评论时间：${item.time}</span>
            <span class="badge" style="background-color: #5BC0DE;">评论人：${item.username}</span>
          </li>
        `
        // zpage=res.allPage
        // console.log(zpage);
      })
      // console.log(res);
      document.querySelector('ul').innerHTML = str
      document.querySelector('.page-show').innerHTML = page
    }
    xuanran()

    // 请求新增
    async function newdata(data) {
      let datanew = await axios({
        method: 'POST',
        url: 'https://hmajax.itheima.net/api/addcmt',
        data: data
      })
      // console.log(datanew);
    }

    // 给文本框设置点击事件，拿到文本框的文本值给请求函数
    document.querySelector('.btn').addEventListener('click', async function (e) {
      e.preventDefault()
      if (document.querySelector('.username').value === '' || document.querySelector('.content').value === '') {
        pxmu.fail('输入的值不能为空哦')
      } else {
        const from = document.querySelector('.cmt-form')
        // let data = serialize(form, { hash: true, empty: true })
        let data = {
          username: document.querySelector('.username').value,
          content: document.querySelector('.content').value
        }
        // await 等待这个函数调用完有结果 再去渲染页面
        await newdata(data)
        xuanran()
        document.querySelector('.username').value = ''
        document.querySelector('.content').value = ''
      }
    })
    xuanran()

    // 删除评论
    function removeData() {
      document.querySelector('.mt15.list-group').addEventListener('click', async function (e) {
        // console.log(e.target.dataset.id)
        let id = e.target.dataset.id
        if (e.target.dataset.id) {
          try {
            pxmu.loading('删除中...')
            let res = await axios.get('https://hmajax.itheima.net/api/delcmt', { params: { id: id } })
            pxmu.loading('删除完毕')
            pxmu.closeload()
            // console.log(res.data.allPage);
            zpage = res.data.allPage
            // console.log(res);
            if (page > zpage) page = zpage
            xuanran()
          } catch {
            pxmu.fail('删除失败')
          }
        }
      })
    }
    removeData()
    xuanran()


    // setInterval(res, 500)
    // async function res() {
    //   let res = await getName()
    //   let res1 = res.data
    //   // console.log(res1);
    //   res1.forEach(item => {
    //     axios.get(`https://hmajax.itheima.net/api/delcmt?id=${item.id}`)
    //   })
    // }
    // res()

  </script>
</body>

</html>